package com.atguigu.guli.service.edu.service.impl;

import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.query.CourseSeacherTwo;
import com.atguigu.guli.service.edu.entity.query.CourseSearch;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailsVo;
import com.atguigu.guli.service.edu.entity.vo.CoursePublishVo;
import com.atguigu.guli.service.edu.entity.vo.CourseVO;
import com.atguigu.guli.service.edu.mapper.CourseDescriptionMapper;
import com.atguigu.guli.service.edu.mapper.CourseMapper;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-07-26
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    /**
     * 保存课程和课程详情信息
     * @param courseInfoForm
     * @return 新生成的课程id
     */
    @Autowired
    private CourseDescriptionMapper courseDescriptionMapper;

    @Override
    public String saveCourseInfo(CourseInfoForm courseInfoForm) {
        //保存课程基本信息

        Course course = new Course();
        course.setStatus(Course.COURSE_DRAFT);
        BeanUtils.copyProperties(courseInfoForm,course);
        baseMapper.insert(course);
        //保存课程详情信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(course.getId());
        courseDescriptionMapper.insert(courseDescription);

        return course.getId();
    }

    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm, String courseId) {
        //更新课程信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfoForm,course);
        course.setId(courseId);
        baseMapper.updateById(course);

        //更新课程简介信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseId);
        courseDescriptionMapper.updateById(courseDescription);

    }

    @Override
    public Page<CourseVO> getListByCondition(Long pageNum, Long pageSize, CourseSearch courseSearch) {
        String subjectId = courseSearch.getSubjectId();
        String subjectParentId = courseSearch.getSubjectParentId();
        String teacherId = courseSearch.getTeacherId();
        String title = courseSearch.getTitle();

        Page<CourseVO> page = new Page<>(pageNum,pageSize);
        //用到自定义的javabean，查询的数据集合是CourseVO对象的集合，因关联到多张表
        //可自定义sql将查询到的结果集封装为CourseVO对象的集合
        //如果mapper的自定义方法第一个传入的是page对象，之前配置的分页拦截器会自动添加分页条件
        //Page: 携带分页数据，如果传入了page对象，我们自己不用使用它，mp的分页拦截器会自动使用
        //QueryWrapper: 用来携带 查询条件，在自己sql中需要，通过queryWrapper对象的 ${ew.customSqlSegment} 获取条件字符串使用

        QueryWrapper<CourseVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("t1.publish_time");

        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("t1.title",title);
        }
        if (!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("t1.teacher_id",teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.like("t1.subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)){
            queryWrapper.like("t1.subject_id",subjectId);
        }


        List<CourseVO> records = baseMapper.selectPageByCourseSearch(page,queryWrapper);
        page.setRecords(records);
        return page;

    }

    @Override
    public CoursePublishVo getCoursePublishVoById(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishVoById(id);
        return coursePublishVo;
    }

    @Override
    public boolean publishCourseById(String id) {
        Course course = new Course();
        course.setId(id);
        course.setPublishTime(new Date());
        course.setStatus(Course.COURSR_NORMAL);
        //baseMapper.updateById(course);  这两个有区别吗
        return this.updateById(course);
    }

    @Override
    public List<Course> listCourses(CourseSeacherTwo courseSeacher) {

        String subjectParentId = courseSeacher.getSubjectParentId();
        String subjectId = courseSeacher.getSubjectId();
        String sortColumn = courseSeacher.getSortColumn();
        Integer type = courseSeacher.getType();

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status","Normal");
        if (!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)){
            queryWrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(sortColumn)){
            if (type == 0){
                //升序
                queryWrapper.orderByAsc(sortColumn);
            }else{
                //降序
                queryWrapper.orderByDesc(sortColumn);
            }
        }
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public CourseDetailsVo getCourseDetails(String id) {

        CourseDetailsVo courseDetailsVo = baseMapper.getCourseDetails(id);
        //每访问一次,更新浏览量
        courseDetailsVo.setViewCount(courseDetailsVo.getViewCount()+1);//更新显示的
        Course course = new Course();
        course.setId(courseDetailsVo.getId());
        course.setViewCount(courseDetailsVo.getViewCount());//更新数据库课程信息的
        baseMapper.updateById(course);

        return courseDetailsVo;
    }

    @Cacheable(value = "index",key = "'selectHotCourse'")
    @Override
    public List<Course> selectHotCourse() {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("view_count");
        queryWrapper.last("limit 8");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public CourseDto getCourseDtoById(String courseId) {
        return baseMapper.selectCourseDtoById(courseId);
    }

    @Override
    public void updateBuyCountById(String courseId) {
        Course course = baseMapper.selectById(courseId);
        course.setBuyCount(course.getBuyCount()+1);
        this.updateById(course);
    }
}
